$.getScript("../../../common/js/jquery.cookie.min.js", function() {
	$("#header").load( "../../../common/page/header.html", function() {
				$("#left-panel").load("../../../common/page/navigation.html", function() {
							getCurrentUser();
						});
				$.getScript("../../../common/js/app.min.js", function() {
					pageSetUp();
					loadCustomerInfo();
					$('.activity-dropdown').click(function() {
						$('.btn-group-justified input').click();
					});
				});
			});
});

function userLogout(){
	cleanUserCookie();
	var url = "/server/session/logout.json";
	$.post(url, function(rtn){
	})
}

$("#kf_list").attr("style","display: none;right:20px");

var memberId;
var departmentId;
var groupLevel;
var customerId;
function getCurrentUser(){
	var url = "/server/session/current.json";
	$.ajax({
		url: url,
		type: "post",
		error: function(jqXHR, textStatus, errorMsg) {
			if ($.cookie("rmbUser") == "true"){
			}else{
				alert("您还未登录！");
			}
             var refURL = window.location.href;
             window.location.href = "../../../user/login/page/login.html?ref="+refURL;
		}
	}).success(function(rtn) {
		memberId = rtn.id;
		departmentId = rtn.departmentId;
		customerId=rtn.customerId;
		$('#admin-name').text("姓名：" + rtn.realName);
		$('#admin-header-pic').attr("src",rtn.headerUrl);
		//标记  &没有发现哪个地方有用到
//		$('#NickName').attr('title', rtn.nickName);
//		$('#RealName').attr('title', rtn.realName);
//		$('#UserAccount').attr('title', rtn.userAccount);
//		$('#Password').attr('title', rtn.password);
		
		loadNavigation();
	});
}
function initPageInfo(){}
function loadNavigation() {
	$.cookie("testsignup", false, {
		path : "/"
	});

	$.cookie("coursesignup", false, {
			path : "/"
	});
	
	var url = "/server/system/member/group/navigation.json";
	$.getJSON(url,function(rtn){
		var page = {};
		page.content = rtn;
		var datalist = navigation_html(page);
		$('#navigation-list').html(datalist);
		loadMemberRole();
		//所有时间控件设为只读
		$('.hasDatepicker').prop('readonly', true); 
		
		count();
	});
}

function loadMemberRole(){
	var url = "/server/system/member/group/list.json";
	var data = {member_id:memberId};
	$.getJSON(url,data,function(rtn){
		if(rtn!=null){
			var level = rtn[0].group.level;
			groupLevel =level;
			if(level=="SYSTEM"){
				var data;
				var url = "/server/department/info/list.json?parentId=super";
				$.getJSON(url,data,function(rtn){
					$.each(rtn,function(index,item){
						departmentId = item.id;
					})
					initPageInfo();
				})
			}else{
				initPageInfo();
			}
		}
		
	});
}

function navigation_html(page){
	var tpl=[
		 		'{@each content as it,k}',
		 		'<li>',
	                '<a href="javascript:;"><i class="fa fa-lg fa-fw ${it.moduleIcon}"></i> <span class="menu-item-parent">${it.moduleName}</span></a>',
	                '<ul>',
	                	'{@each it.resources as it1,j}',
		                	'<li>',
		                        '<a href="javascript:;"> ${it1.moduleName}</a>',
		                        '<ul>',
		                        	'{@each it1.resources as it2,i}',
			                            '<li>',
			                                '<a href="${it2.enterUrl}"> ${it2.moduleName}</a>',
			                            '</li>',
			                            '{@if it2.enterUrl=="../../../train/online_course/page/signup.html"}',
			                            '$${it2.enterUrl|link_build}',
			                            '{@/if}',
			                            '{@if it2.enterUrl=="../../../exam/test/page/signup.html"}',
			                            '$${it2.enterUrl|link_build}',
			                            '{@/if}',
		                            '{@/each}',
		                        '</ul>',
		                    '</li>',
	                	'{@/each}',
	                '</ul>',
	            '</li>',
		 		'{@/each}',
		 	].join('\n');
		 	return juicer(tpl,page);
}
var link = function(data) {
	if (data == "../../../exam/test/page/signup.html") {
		$.cookie("testsignup", true, {
			path : "/"
		});
	}
	if (data == "../../../train/online_course/page/signup.html") {
		$.cookie("coursesignup", true, {
			path : "/"
		});
	}

};
juicer.register('link_build', link); //注册自定义函数

//加载商户信息到

function loadCustomerInfo(){
	var url = "/server/customer/info/detail.json";
	$.getJSON(url,function(rtn){
		$('.page-footer span:eq(0)').html(rtn.copyRight);
		$('.project-context span:eq(0)').html(rtn.welcomeMessage);
		$('#logo img').attr("src", rtn.logo);
		$('title').html(rtn.title);
		//加载用户标记
		$('#SiteUrl').attr('title', rtn.url);
		$('#CustomerTitle').attr('title', rtn.title);
		$('#CustomerName').attr('title', rtn.customerName);
		$('#CustomerCode').attr('title', rtn.customerCode);
		$('#CustomerLogo').attr('title', rtn.logo);
		$('#CustomerWelcomMSG').attr('title', rtn.welcomeMessage);
		$('#Customer-Name').text(rtn.customerName);
		
		//管理端跳学员端地址
		if(rtn.theme!=null){
			$("#admin-to-student").attr("href",rtn.theme);
		}
		if(rtn.url!=null){
			$("#admin-loginout").attr("href",rtn.url);
		}
	});
}

function count(){
	var data={
			testsignup:$.cookie("testsignup"),
			coursesignup:$.cookie("coursesignup")
	}
	var url = "/server/member/info/countMailsForAdmin.json";
	jQuery.getJSON(url,data,function(rtn) {
		$('.activity-dropdown b').html(rtn.total);
		$('.btn-group-justified label').html("管理员任务("+rtn.total+")");
		var total = rtn.auditUsers_del+rtn.auditUsers_add;
		var lis = "";
		if (rtn.auditCourseForSignup > 0) {
			lis += "<li class=''>当前还有&nbsp;<span class='audit-course-signup'>"
					+ rtn.auditCourseForSignup
					+ "</span>&nbsp;个[课程报名]需要您审核&nbsp;&nbsp;&nbsp;&nbsp;"
					+ "<a href = '../../../train/online_course/page/signup.html'>转到>></a></li>";
		}

		if (rtn.auditTestingForSignup > 0) {
			lis += "<li class=''>当前还有&nbsp;<span class='audit-test-signup'>"
					+ rtn.auditTestingForSignup
					+ "</span>&nbsp;个[考试报名审核]需要您审核&nbsp;&nbsp;&nbsp;&nbsp;"
					+ "<a href = '../../../exam/test/page/signup.html'>转到>></a></li>";
		}
		var $ol_li = jQuery("<ol>"+lis+"</ol>");
		jQuery('.ajax-notifications').html($ol_li);
	});
}

//清除自动登录和记住密码
function cleanUserCookie(){
	 
	$.cookie("rmbUser", "false", {
		path : '/',
		expires: -1
	});
	$.cookie("loginid", "", {
		path : '/',
		expires: -1
	});
	$.cookie("lmsp", "", {
		path : '/',
		expires: -1
	});
	
	$.cookie("automaticlogin", false, {
		path : '/',
		expires: -1
	});
}
